package v1

import (
	"auth/authentication"
	log "auth/collector/logger"
	"auth/server/web/middleware"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

var OAuth = &oAuth{}

type oAuth struct{}

func (a *oAuth) Init(r *gin.RouterGroup) {
	r.Group("", middleware.Auth() /*, middleware.CasbinHandler()*/).
		GET("/oauth", a.oAuth)
}

func (a *oAuth) oAuth(c *gin.Context) {
	userId, err := authentication.ContextUserID(c.Request.Context())
	if err != nil {
		log.Error("oAuth error", err)
		c.JSON(http.StatusUnauthorized, nil)
		return
	}
	if userId > 0 {
		c.Header("X-UserID", strconv.FormatInt(userId, 10))
	}
	c.JSON(http.StatusOK, "ok")
}
